package com.borderbears.backend.gui.dockables;

import java.awt.BorderLayout;
import java.awt.Component;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.vlsolutions.swing.docking.DockKey;
import com.vlsolutions.swing.docking.Dockable;

public class LogPanel extends JPanel implements Dockable {

	private final DockKey dockKey;

	public LogPanel(final DockKey key) {
		dockKey = key;
		setLayout(new BorderLayout());

		
		final JTextArea taLog = new JTextArea();
		taLog.setEditable(false);
		Logger.getLogger("").addHandler(new Handler() {
			@Override
			public void publish(final LogRecord record) {
				taLog.append(record.getSourceClassName()+"\n"+record.getLevel().getName()+": "+record.getMessage()+"\n\n");
				taLog.setCaretPosition(taLog.getDocument().getLength());
			}

			@Override
			public void close() throws SecurityException {}

			@Override
			public void flush() {}
		});
		final JScrollPane spLog = new JScrollPane(taLog);
		add(spLog);
	}

	public Component getComponent() {
		return this;
	}

	public DockKey getDockKey() {
		return dockKey;
	}
}
